package com.second.app.thread.p_c_test;

/**
 * @author gyz
 * @date 2021/1/25 17:21
 */
public class Produce {

    private String lock;

    public Produce(String lock) {

        this.lock = lock;
    }

    public void setValue() {
        try {
            synchronized (lock) {
                //假设value有值.让生产线程等待
                if (!ValueObject.getValue().equals("")) {
                    lock.wait();
                }
                //否则，设置value值后
                String value = System.currentTimeMillis() + "_" + System.nanoTime();
                System.out.println(Thread.currentThread().getName() + ":设置值" + value);
                ValueObject.setValue(value);
                //成功后,通知消费线程去消费。
                lock.notify();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
